package day26;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Scanner;

public class HomeWork {}

class A3{
    private A3(Integer a){
        System.out.println("ok,A3");
        System.out.println("正在运行有参构造函数"+a);
    }
}

class A3Test{
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class c = Class.forName("day26.A3");
        Constructor con = c.getDeclaredConstructor(Integer.class);
        con.setAccessible(true);
        con.newInstance(1);
    }
}

class B3{
    private String[] CreateString(){
        String[] str = new String[5];
        str[0] = "A";
        str[1] = "B";
        str[2] = "C";
        return str;
    }
}

class B3Test{
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class c2 = Class.forName("day26.B3");
        Method m2 = c2.getDeclaredMethod("CreateString");
        B3 b = new B3();
        m2.setAccessible(true);
        Object result = m2.invoke(b);
        String[] str = (String[]) result;
        System.out.println(Arrays.toString(str));
    }
}

class A4{
    private Integer id;
    private String name;

    public A4(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class A4Test{
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        A4 a4 = new A4(18,"李明");
        Class c = Class.forName("day26.A4");
        Field[] fs = c.getDeclaredFields();
        for (Field f : fs) {
            String fieldName = f.getName();
            getProperty(a4,fieldName);
        }//获取对象属性值
        for(Field f : fs){
            String fieldName = f.getName();
            System.out.println("请输入"+fieldName+"更改后的值:");
            setProperty(a4,fieldName,sc.next());
        }//更改对象属性值
        System.out.println("ID:"+a4.getId()+" "+"姓名:"+a4.getName());
    }

    static void setProperty(Object object, String property, Object value) throws ClassNotFoundException, NoSuchFieldException {
        Class c = Class.forName("day26.A4");
        Field fs = c.getDeclaredField(property);
        Class type = fs.getType();
        fs.setAccessible(true);
        try {
            if (type == Integer.class) {
                fs.set(object, Integer.valueOf((String) value));
            } else if (type == Double.class) {
                fs.set(object, Double.valueOf((String) value));
            } else if (type == Boolean.class) {
                fs.set(object, Boolean.valueOf((String) value));
            } else if (type == String.class) {
                fs.set(object, String.valueOf(value));
            } else {
                System.out.println("不支持的类型: " + type.getSimpleName());
            }
        }
        catch (NumberFormatException | IllegalAccessException e){
            e.printStackTrace();
        }
        System.out.println("更改成功");
    }

    static void getProperty(Object object, String property) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
        Class c = Class.forName("day26.A4");
        Field fs = c.getDeclaredField(property);
        fs.setAccessible(true);
        Object value = fs.get(object);
        System.out.println("更改前"+property+":"+value);
    }
}

class B4{
    private Integer id;
    private String name;
    public B4(){
        System.out.println("ok,B4");
    }

    public B4(Integer id, String name) {
        System.out.println("ok,B4");
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class B4Test{
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        File f = new File("D:\\c\\result.txt");
        if(!f.exists()){
            File fp = f.getParentFile();
            if(!fp.exists()){
                fp.mkdirs();
            }
            f.createNewFile();
        }
        FileWriter fw = new FileWriter(f);

        Class c = Class.forName("day26.B4");
        System.out.println("----普通方法----");
        Method[] m = c.getDeclaredMethods();
        for (Method m1 : m) {
            String methodName = m1.getName();
            Class[] cs = m1.getParameterTypes();
            Class returnType = m1.getReturnType();
            System.out.println("方法名:"+methodName+
                    "\t参数:"+Arrays.toString(cs)+
                    "\t返回值类型:"+returnType);
            fw.write("方法名:"+methodName+
                    "\t参数:"+Arrays.toString(cs)+
                    "\t返回值类型:"+returnType+"\n");
        }

        System.out.println("----构造方法----");
        Constructor[] cons = c.getDeclaredConstructors();
        for (Constructor c1 : cons) {
            String methodName = c1.getName();
            Class[] cs = c1.getParameterTypes();
            System.out.println("方法名:"+methodName+
                    "\t参数:"+Arrays.toString(cs));
            fw.write("方法名:"+methodName+
                    "\t参数:"+Arrays.toString(cs)+"\n");
        }

        System.out.println("------获取属性--------");
        Field[] fs=c.getDeclaredFields();
        for(Field f1:fs){
            String fieldName=f1.getName();
            Class type=f1.getType();
            System.out.println("属性名:"+fieldName+",类型:"+type);
            fw.write("属性名:"+fieldName+",类型:"+type+"\n");
        }
        fw.close();
    }
}